package com.qfedu.user.controller;

import com.qfedu.common.annotation.NoLoginCheck;
import com.qfedu.common.entity.User;
import com.qfedu.common.utils.JwtUtils;
import com.qfedu.common.utils.R;
import com.qfedu.user.param.LoginParam;
import com.qfedu.user.service.LoginService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 2025/1/13下午8:53
 */
@Validated
@RestController
@Slf4j
@RequestMapping("/user")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 登录
     */
    @NoLoginCheck
    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginParam loginParam) {
        System.out.println("loginParam = " + loginParam);

        Object token = loginService.login(loginParam);

        return R.ok(token);
    }


    /**
     *info 信息
     * 像前端传递用户信息
     */
    @NoLoginCheck
    @GetMapping("/Info")
    public R getUserInfo(String token) {
        log.debug(token);

        Claims claims = JwtUtils.parseJWT(token);



        Integer id = (Integer) claims.get("id");
        String username = (String) claims.get("username");
        String  avatar = (String) claims.get("avatar");

        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setAvatar(avatar);


        return R.ok(user);


    }



    /**
     * 登出
     */
    @NoLoginCheck
    @PostMapping("/logout")
    public R logout() {
        return R.ok();
    }
}
